package com.huatai.bi.controller;

import com.huatai.bi.dto.ColumnDataDto;
import com.huatai.bi.dto.DataSetCreateDto;
import com.huatai.bi.dto.DataSetInfoDto;
import com.huatai.bi.dto.DataSetListDto;
import com.huatai.bi.entity.DataSetParameterEntity;
import com.huatai.bi.handler.DataSetHandler;
import com.huatai.bi.service.DataSetParameterService;
import com.huatai.bi.service.DirectDataSetService;
import com.huatai.bi.service.impl.DataSetCommonService;
import com.huatai.common.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "数据集API",tags = "数据集管理")
@RequestMapping("/dataSet")
public class DataSetController {

    @Autowired
    DataSetHandler dataSetHandler;
    @Autowired
    DirectDataSetService service;
    @Autowired
    DataSetCommonService dataSetCommonService;
	@Autowired
	DataSetParameterService dataSetParameterService;

    @PostMapping("/save")
    @ApiOperation(value = "新增或修改")
    public R save(@RequestBody DataSetCreateDto dto){
        dataSetHandler.create(dto);
        return R.data(null);
    }

    @PostMapping("/getPreviewData")
    @ApiOperation(value = "预览数据")
    public R previewData(@RequestBody DataSetCreateDto entity){
        return R.data(dataSetHandler.previewData(entity));
    }

    @GetMapping("/getPreviewData/{id}")
    @ApiOperation(value = "预览数据")
    public R previewDataById(@PathVariable("id") Long id){
        return R.data(dataSetHandler.previewData(id));
    }


    @GetMapping("/getTables/{dsId}/{dtId}")
    @ApiOperation(value = "DB数据集表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dsId", value = "数据源ID",dataType = "Long"),
            @ApiImplicitParam(name = "dtId", value = "目录ID",dataType = "Long")
    })
    public R getTables(@PathVariable("dsId") Long dsId,@PathVariable("dtId") Long dtId){
        return R.data(service.getTables(dsId,dtId));
    }

    @PostMapping("/columnData")
    @ApiOperation(value = "获取数据集字段值")
    public R columnData(@Valid @RequestBody ColumnDataDto dto){
        return R.data(service.columnData(dto));
    }

    @GetMapping("/dataInfo/{id}")
    @ApiOperation(value = "数据详情")
    public R<DataSetInfoDto> dataInfo(@PathVariable("id") Long id){
        return R.data(service.dataInfo(id));
    }

    @PostMapping("/list")
    @ApiOperation(value = "数据集列表")
    public R<List<DataSetListDto>> list(){
        return R.data(service.dataSetList());
    }

    @GetMapping("/useStatus/{id}")
    @ApiOperation(value = "数据集使用状态")
    public R<Boolean> useStatus(@PathVariable("id") Long id){
        boolean useFlag = dataSetCommonService.isUseDataSetById(id);
        String msg = "操作成功";
        if(useFlag){
            msg = "数据集已被BI组件使用,请谨慎修改";
        }
        return R.data(useFlag,msg);
    }

    @GetMapping("/editInfo/{id}")
    @ApiOperation(value = "编辑详情")
    public R<DataSetCreateDto> editInfo(@PathVariable("id") Long id){
        return R.data(service.info(id));
    }

	@GetMapping("/params/{dtId}")
	@ApiOperation(value = "获取数据集参数")
	public R<List<DataSetParameterEntity>> params(@PathVariable("dtId") Long dtId){
		return R.data(dataSetParameterService.listByDtId(dtId));
	}
}
